package tom.client.invoice;

import java.util.Map;

import tom.client.DataForm;
import tom.client.Library;
import tom.client.LookupDialog;
import tom.shared.ContactGroup;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Label;

public class FormContactGroup extends DataForm {
	private static FormContactGroup panel;

	public static FormContactGroup getPanel() {
		if (panel == null)
			panel = GWT.create(FormContactGroup.class);
		return panel;
	}

	private ContactGroup record;

	public FormContactGroup() {
		lbCode.setText("รหัสกลุ่ม");

		txtGrpName = new TextBox();
		add(txtGrpName, 122, 114);
		txtGrpName.setSize("150px", "18px");

		txtNote = new TextArea();
		add(txtNote, 122, 154);
		txtNote.setSize("181px", "110px");

		Label lblNewLabel = new Label("ชื่อกลุ่ม");
		add(lblNewLabel, 25, 114);

		Label lblNewLabel_1 = new Label("หมายเหตุ");
		add(lblNewLabel_1, 23, 154);
		lblNewLabel_1.setSize("56px", "26px");
		setFieldsHandler();
	}

	@Override
	protected void search() {
		LookupDialog.show("ประเภท ที่ติดตอ่", "grpId", txtCode);
	}

	@Override
	protected void refresh() {
		Library.getSaleService().selectContGroup(
				Integer.valueOf(txtCode.getText()), dataCallback);
	}

	@Override
	protected void update() {
		getFields();
		Library.getSaleService().updateContGroup(record, delCallback);
	}

	@Override
	protected void insert() {
		getFields();
		Library.getSaleService().insertContGroup(record, delCallback);
	}

	@Override
	protected void delete() {
		if (Window.confirm("ต้องการลบ " + txtGrpName.getText() + " หรือไม่?"))
			Library.getSaleService().deleteContGroup(
					Integer.valueOf(txtCode.getText()), delCallback);
	}

	@Override
	protected void clearFields() {
		super.clearFields();
		txtGrpName.setText(null);
		txtNote.setText(null);
	}

	@Override
	protected void setFields() {
		super.setFields();
		if (record != null) {
			txtGrpName.setText(record.grpName);
			txtNote.setText(record.note);
		} else {
			String code = txtCode.getText();
			clearFields();
			super.setMode(3);
			txtCode.setText(code);
		}
	}

	@Override
	protected void getFields() {
		if (record == null)
			record = new ContactGroup();
		record.grpId = Integer.valueOf(txtCode.getText());
		record.grpName = txtGrpName.getText();
		record.note = txtNote.getText();
	}

	@Override
	protected String getReportFile() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected Map<String, Object> genReportParams() {
		// TODO Auto-generated method stub
		return null;
	}

	AsyncCallback<ContactGroup> dataCallback = new AsyncCallback<ContactGroup>() {

		@Override
		public void onSuccess(ContactGroup result) {
			record = result;
			setFields();
		}

		@Override
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}
	};
	private final TextBox txtGrpName;
	private final TextArea txtNote;
}
